#!/usr/bin/env python

import wx

class Canvas(wx.ScrolledWindow):
    '''Canvas class
    
    @author Julot <andy.julot@gmail.com>
    @since Wednesday, February 29, 2012. 10:33 PM'''
    
    def __init__(self, parent):
        wx.ScrolledWindow.__init__(self, parent, style=wx.BORDER_THEME)
        
        self.SetBackgroundColour('WHITE')
        self.SetScrollRate(20, 20)
        
        self.pdc = wx.PseudoDC()
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
    
    def OnPaint(self, event):
        # Create a buffered paint DC.  It will create the real
        # wx.PaintDC and then blit the bitmap to it when dc is
        # deleted.  
        dc = wx.BufferedPaintDC(self)
        # use PrepareDC to set position correctly
        self.PrepareDC(dc)
        # we need to clear the dc BEFORE calling PrepareDC
        bg = wx.Brush(self.GetBackgroundColour())
        dc.SetBackground(bg)
        dc.Clear()
        # create a clipping rect from our position and size
        # and the Update Region
        xv, yv = self.GetViewStart()
        dx, dy = self.GetScrollPixelsPerUnit()
        x, y = (xv * dx, yv * dy)
        rgn = self.GetUpdateRegion()
        rgn.Offset(x,y)
        r = rgn.GetBox()
        # draw to the dc using the calculated clipping rect
        self.pdc.DrawToDCClipped(dc, r)
        #event.Skip()
